home *** CD-ROM | disk | FTP | other *** search
/ Java Developer's Companion / Java Developer's Companion.iso / binaries / Windows / jsdk / src / sun / servlet / apache / NcgiServletGate.java < prev    next >
Encoding:
Java Source  |  1997-07-18  |  5.1 KB  |  213 lines

  1. // NcgiServletGate - NCGI program that runs Servlets
  2. //
  3. // Copyright (c) 1996 Sun Microsystems, Inc.  All Rights reserved
  4. // Permission to use, copy, modify, and distribute this software
  5. // and its documentation for NON-COMMERCIAL purposes and without
  6. // fee is hereby granted provided that this copyright notice
  7. // appears in all copies. Please refer to the file copyright.html
  8. // for further important copyright and licensing information.
  9. //
  10. // SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF
  11. // THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
  12. // TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
  13. // PARTICULAR PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR
  14. // ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
  15. // DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
  16.  
  17. package sun.servlet.apache;
  18.  
  19. import java.io.*;
  20. import java.util.*;
  21. import java.net.*;
  22. import javax.servlet.*;
  23. import javax.servlet.http.*;
  24.  
  25. /** NCGI program that runs Servlets.
  26. ** <P>
  27. ** This is an NCGI program in java, but instead of actually serving
  28. ** content it instead turns the NCGI requests into Jeeves-compatible
  29. ** Servlet requests, and then runs the Servlets.
  30. */
  31.  
  32. public class NcgiServletGate extends ServletGate {
  33.     
  34.     private static final int PORT = 31461;
  35.     private static final String AUTHFILE = "/tmp/ncgiauth";
  36.     
  37.     public static void main( String[] args ) {
  38.     
  39.     // Parse args.
  40.     int port = PORT;
  41.     String authfile = AUTHFILE;
  42.     String servletPropFile = null;
  43.     int argc = args.length;
  44.     int argn;
  45.     for ( argn = 0; argn < argc && args[argn].charAt( 0 ) == '-'; ++argn ) {
  46.         if ( args[argn].equals( "-p" ) && argn + 1 < argc ) {
  47.         ++argn;
  48.         port = Integer.parseInt( args[argn] );
  49.         }
  50.         else if ( args[argn].equals( "-a" ) && argn + 1 < argc ) {
  51.         ++argn;
  52.         authfile = args[argn];
  53.         }
  54.         else if (args[argn].equals( "-s" ) && argn + 1 < argc ) {
  55.         ++argn;
  56.             servletPropFile = args[argn];
  57.         }
  58.         else usage();
  59.     }
  60.     if ( argn != argc ) usage();
  61.     
  62.     // Create the server.
  63.     NcgiServletGate serve = new NcgiServletGate(port, authfile, 
  64.                             servletPropFile );
  65.     
  66.  
  67.     // And run.
  68.     serve.serve();
  69.  
  70.     // If we get here, we're done.
  71.     System.exit( 0 );
  72.     }
  73.  
  74.     private static void usage() {
  75.     System.err.println( "usage:  NcgiServletGate [-p port] [-a authfile]" );
  76.     System.exit( 1 );
  77.     }
  78.  
  79.     private int port;
  80.     private String authfile;
  81.  
  82.     /** 
  83.      * Constructor.
  84.      */
  85.     public NcgiServletGate( int port, String authfile, String servletPropFile ) {
  86.     super();
  87.     this.port = port;
  88.     this.authfile = authfile;
  89.     
  90.     // load initial servlet properties
  91.     if (servletPropFile != null) {
  92.         loadServletProps(servletPropFile);
  93.     }
  94.     
  95.     }
  96.     
  97.     /** 
  98.      *  Run the server.  Returns only on errors.
  99.      */
  100.     public void serve() {
  101.     NcgiServer ncgiServer = null;
  102.         try {
  103.         ncgiServer = new NcgiServer( port, authfile );
  104.             while ( true ) {
  105.                 Socket socket = ncgiServer.accept();
  106.         try {
  107.             InputStream in = socket.getInputStream();
  108.             OutputStream out = socket.getOutputStream();
  109.             new NcgiServletGateConnection( this, in, out, 
  110.                            ncgiServer, socket );
  111.         }
  112.         catch ( IOException e ) {
  113.             log( "problem getting streams: " + e.toString() );
  114.         }
  115.         }
  116.     }
  117.         catch ( NcgiException e ) {
  118.             log( e.toString() );
  119.     }
  120.         finally {
  121.         if ( ncgiServer != null )
  122.         ncgiServer.done();
  123.         destroyAllServlets();
  124.     }
  125.     }
  126.  
  127.  
  128.     // Methods from ServletContext.
  129.  
  130.     /** 
  131.      * The name of this software.
  132.      */
  133.     public String serverName() {
  134.     return "sun.servlet.apache.ncgi.NcgiServletGate";
  135.     }
  136.  
  137.     /** 
  138.      * The version of this software.
  139.      */
  140.     public String serverVersion() {
  141.     return "v1.0beta1";
  142.     }
  143.     
  144.     /** 
  145.      *  The URL for this software.
  146.      */
  147.     public String serverUrl() {
  148.     return "http://jeeves.javasoft.com/";
  149.     }
  150.     
  151. }
  152.  
  153.  
  154. class NcgiServletGateConnection extends ServletGateConnection 
  155.                                 implements Runnable {
  156.     private NcgiServer ncgiServer;
  157.     private Socket socket;
  158.  
  159.     /** 
  160.      * Constructor.
  161.      */
  162.     public NcgiServletGateConnection( ServletGate serve, InputStream in, 
  163.                      OutputStream out, NcgiServer ncgiServer, 
  164.                      Socket socket ) {
  165.     super( serve, in, out );
  166.  
  167.     // Save arguments.
  168.     this.ncgiServer = ncgiServer;
  169.     this.socket = socket;
  170.     
  171.     // Start a separate thread to read and handle the request.
  172.     Thread thread = new Thread( this );
  173.     thread.start();
  174.     }
  175.  
  176.  
  177.     // Methods from Runnable.
  178.     
  179.     private NcgiRequest ncgiRequest;
  180.     
  181.     public void run() {
  182.     try {
  183.         // Read the request.
  184.         ncgiRequest = new NcgiRequest( in, ncgiServer );
  185.  
  186.         // Handle it.
  187.         handle();
  188.     }
  189.     catch ( NcgiException e ) {
  190.             serve.log( e.toString() );
  191.     }
  192.     try {
  193.         socket.close();
  194.     }
  195.     catch ( IOException ignore ) { }
  196.     }
  197.  
  198.     /** 
  199.      * Returns a particular variable for this request.
  200.      */
  201.     public String getVar( String name ) {
  202.     return ncgiRequest.getVar( name );
  203.     }
  204.     
  205.     /**
  206.      * Returns an enumeration of variables for this request.
  207.      */
  208.     public Enumeration getVars() {
  209.         return ncgiRequest.getVars();
  210.     }
  211.  
  212. }
  213.